package com.atguigu.gmall.common.config.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author atguigu-mqx
 */
@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //  编写一个发送消息的方法！
    public boolean sendMessage(String exchange,String routingKey, Object message){
        //  发送消息！
        this.rabbitTemplate.convertAndSend(exchange,routingKey,message);

        return true;
    }

    //  编写一个发送延迟消息的方法
    public boolean sendDelayMessage(String exchange,String routingKey, Object message, int delayTime){
        //  发送消息！
        this.rabbitTemplate.convertAndSend(exchange,routingKey,message,msg -> {
            //  消息TTL
            msg.getMessageProperties().setDelay(delayTime*1000);
            return msg;
        });

        return true;
    }

}
